package pl.edu.agh.neuraleconomy.persistence.exchange;

import java.sql.SQLException;

import pl.edu.agh.neuraleconomy.common.exception.ApplicationRuntimeException;
import pl.edu.agh.neuraleconomy.model.exchange.Currency;
import pl.edu.agh.neuraleconomy.persistence.base.BaseDao;

public class CurrencyDao extends BaseDao<Currency>{

	public CurrencyDao() {
		super(Currency.class);
	}
	
	public Currency getByName(String name){
		try {
			return getFirst(dao.queryForEq("name", name));
		} catch (SQLException e) {
			logger.error("Nie udalo sie odczytac obiektu Currency o nazwie " + name, e);
			throw new ApplicationRuntimeException(e);
		}
	}
	
	public Currency getByNameOrCreate(String name) {
		Currency currency = getByName(name);
		if (currency == null) {
			currency = new Currency();
			currency.setName(name);
			create(currency);
		}
		return currency;
	}

}
